using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class SkincareController : ControllerBase
    {
        private readonly ISkincareService _skincareService;

        public SkincareController(ISkincareService skincareService)
        {
            _skincareService = skincareService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<Skincare>> GetSkincare(int id)
        {
            var skincare = await _skincareService.GetSkincareByIdAsync(id);
            if (skincare == null)
            {
                return NotFound();
            }
            return skincare;
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<IEnumerable<Skincare>>> GetSkincareByUserId(int userId)
        {
            var skincare = await _skincareService.GetSkincareByUserIdAsync(userId);
            return Ok(skincare);
        }

        [HttpGet("user/{userId}/category/{category}")]
        public async Task<ActionResult<IEnumerable<Skincare>>> GetSkincareByCategory(int userId, string category)
        {
            var skincare = await _skincareService.GetSkincareByCategoryAsync(userId, category);
            return Ok(skincare);
        }

        [HttpGet("user/{userId}/withproducts")]
        public async Task<ActionResult<IEnumerable<Skincare>>> GetSkincareWithProducts(int userId)
        {
            var skincare = await _skincareService.GetSkincareWithProductsAsync(userId);
            return Ok(skincare);
        }

        [HttpGet("user/{userId}/stats")]
        public async Task<ActionResult<object>> GetSkincareStats(int userId)
        {
            var stats = await _skincareService.GetSkincareStatsAsync(userId);
            return Ok(stats);
        }

        [HttpPost]
        public async Task<ActionResult<Skincare>> CreateSkincare(Skincare skincare)
        {
            var createdSkincare = await _skincareService.CreateSkincareAsync(skincare);
            return CreatedAtAction(nameof(GetSkincare), new { id = createdSkincare.Id }, createdSkincare);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateSkincare(int id, Skincare skincare)
        {
            if (id != skincare.Id)
            {
                return BadRequest();
            }

            await _skincareService.UpdateSkincareAsync(skincare);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteSkincare(int id)
        {
            await _skincareService.DeleteSkincareAsync(id);
            return NoContent();
        }
    }
}